﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Motorbike.APP.Forms;
using ComponentFactory.Krypton.Toolkit;
using System.Threading;

namespace Motorbike.APP
{
    static class Program
    {
        #region Application events handler

        static void Application_ApplicationExit(object sender, EventArgs args)
        {
            BLL.BllServiceFactory.Instance.SystemService.LoginOut(ApplicationContext.Instance.CurrentUser, ApplicationContext.Instance.CurrentLoginLog);
        }

        static void Application_ThreadException(object sender, ThreadExceptionEventArgs args)
        {
            KryptonMessageBox.Show(args.Exception.Message + "\n", "系统错误：", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs args)
        {
            string message = "系统错误";
            if (args.ExceptionObject is Exception)
            {
                Exception ex = args.ExceptionObject as Exception;
                message = ex.Message + "\n" ;
            }

            KryptonMessageBox.Show(message, "未知异常：", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        #endregion

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            #region Application settings
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            #endregion

            try
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);

                if (new LoginForm().ShowDialog() == DialogResult.OK)
                {
                    Application.Run(new MainApplicationForm());
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("系统运行错误：" + ex.Message + "\n" + ex.StackTrace);
                KryptonMessageBox.Show(ex.Message + "\n ", "错误信息：", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
